<template>
	<view>
		<view style="display: flex;margin: 50rpx auto 100rpx auto;align-items: center;justify-content: center; ">
			<image style="display: flex;width: 60rpx;height: 60rpx;" src="../../static/quankelogo.png" mode=""></image>
			<text style="color: red;font-size: 40rpx;margin-left: 20rpx;">礼品卡激活中心</text>
		</view>
		<radio-group style="display: flex;justify-content: space-between;width: 80%;margin: 50rpx auto 100rpx auto;"
			@change="radioChange">
			<label style="display: flex;align-items: center;" v-for="(item, index) in items" :key="item.value">
				<view>
					<radio :value="item.value" :checked="index === current" />
				</view>
				<view>{{item.name}}</view>
			</label>
		</radio-group>
		<view v-if="current==0" style="display: flex;align-items: center;justify-content: center;margin-top: 50rpx;">
			<view style="color: red;">
				卡号：
			</view>
			<input
				style="display: flex;width:420rpx; padding: 15rpx 25rpx;font-size: 25rpx; border-radius: 9rpx;box-shadow: 1px 1px 5px #c2c2c2;"
				type="text" v-model="kahao" placeholder="请输入卡号" />
		</view>
		<view v-if="current==1" style="display: flex;align-items: center;justify-content: center;margin-top: 50rpx;">
			<view style="color: red;">
				卡号：
			</view>
			<textarea v-model="kahao_duo" placeholder="请输入多个卡号 以 , 或空格隔开"
				style="width: 420rpx; font-size: 25rpx;padding: 15rpx 25rpx; height:180rpx; border-radius: 9rpx;box-shadow: 1px 1px 5px #c2c2c2;"
				maxlength="-1"></textarea>

		</view>
		<view v-if="current==2" style="display: flex;align-items: center;justify-content: center;">
			<view style="color: red;">
				起始卡号：
			</view>
			<input
				style="display: flex;width:420rpx; padding: 15rpx 25rpx;font-size: 25rpx; border-radius: 9rpx;box-shadow: 1px 1px 5px #c2c2c2;"
				type="text" v-model="kahao_start" placeholder="请输入起始卡号" />
		</view>
		<view v-if="current==2" style="display: flex;align-items: center;margin-top:50rpx;justify-content: center;">
			<view style="color: red;">
				终止卡号：
			</view>
			<input
				style="display: flex;width: 420rpx; padding: 15rpx 25rpx;font-size: 25rpx; border-radius: 9rpx;box-shadow: 1px 1px 5px #c2c2c2;"
				type="text" v-model="kahao_end" placeholder="请输入终止卡号" />
		</view>
		<view @click="kahao_jihuo"
			style="display: flex;width: 70%;height:80rpx;justify-content: center;align-items: center; margin: 150rpx auto 20rpx auto;background-color: orangered;border-radius: 15rpx;color: white;">
			立 即 激 活
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				quankezx_userinfo: [],
				kahao_duo: '',
				shuju: [],
				kahao_start: '',
				kahao_end: '',
				kahao: '',
				current: 0,
				items: [{
						value: 'dan',
						name: '单个激活',
						checked: 'true'
					},
					{
						value: 'duo',
						name: '多个激活'
					},
					{
						value: 'qujian',
						name: '区间激活'
					}
				]
			}
		},
		async onLoad() {
			this.quankezx_userinfo = await uniCloud.getCurrentUserInfo()
		},
		methods: {
			get_weijihuo_ka() {
				return new Promise((resolve, reject) => { // 返回Promise对象
					this.shuju = []
					uni.showLoading({
						title: "查询中……"
					})
					if (this.current == 0) {
						this.shuju = [this.kahao]
					} else if (this.current == 1) {
						// 将this.kahao_duo按小写或大写的，分隔，放入数组shuju中
						this.shuju = this.kahao_duo.split(/[，, ]/).filter(item => item.trim() !== '');
					} else {
						// 步骤1：提取前缀和后5位数字
						const prefix = String(this.kahao_start).slice(0, -5); // 保留前缀
						const start = parseInt(String(this.kahao_start).slice(-5), 10); // 后5位转数字
						const end = parseInt(String(this.kahao_end).slice(-5), 10);
						// 步骤2：生成连续数字并补位
						const numArray = [];
						for (let i = Math.min(start, end); i <= Math.max(start, end); i++) {
							numArray.push(i.toString().padStart(5, '0')); // 强制补足5位
						}
						// 步骤3：拼接原始前缀
						this.shuju = numArray.map(num => prefix + num);
					}
					uniCloud.callFunction({
						name: "fun1",
						data: {
							leixing: 5,
							kahao: this.shuju,
							user_id: this.quankezx_userinfo.uid
						}
					}).then(res => {
						uni.hideLoading()
						this.lipinkalist = res.result.data
						console.log("5", res)
						resolve(res) // 异步完成时触发resolve
					}).catch(err => {
						uni.hideLoading()
						reject(err) // 错误时触发reject
					})
				})
			},
			get_myzhekou() {
				return new Promise((resolve, reject) => { // 返回Promise对象
					uniCloud.callFunction({
						name: "fun1",
						data: {
							leixing: 8,
							tiaojian: {
								jingxiaoshang_id: this.quankezx_userinfo.uid
							}
						}
					}).then(res => {
						this.myzhekou_list={}
						uni.hideLoading()
						// 处理返回的数组数据
						res.result.data.forEach(item => {
							// 将每个经销商ID对应的折扣值存入对象
							this.myzhekou_list[item.xinghao_id] = item.zhekou;
						});
						resolve(res) // 异步完成时触发resolve
					}).catch(err => {
						uni.hideLoading()
						reject(err) // 错误时触发reject
					})
				})
			},


			async kahao_jihuo() {
				try {
					await this.get_weijihuo_ka()
					await this.get_myzhekou()
					const sum = this.lipinkalist.reduce((total, item) => total + item.price*this.myzhekou_list[item.leixing_id], 0);
					console.log("e", sum);
					// 把每个卡的余额相加，再按自己级别进行折扣
					// 判断自己的余额是否足够
					// ①把卡号状态改成激活，②减去自己的余额，③做一条预存金额记录,记录好激活的卡号明细，时间和金额
					// 
					// 将this.lipinkalist中的price相加
				} catch (err) {
					console.error('激活流程异常:', err)
					uni.showToast({
						title: '操作失败',
						icon: 'none'
					})
				}
			},
			radioChange: function(evt) {
				for (let i = 0; i < this.items.length; i++) {
					if (this.items[i].value === evt.detail.value) {
						this.current = i;
						console.log(this.current);
						break;
					}
				}
			}
		}
	}
</script>

<style>

</style>